import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import '../../provide/cart.dart';
import '../../provide/person.dart';
import 'package:fluttertoast/fluttertoast.dart';

class CartBottom extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(5.0),
      color: Colors.white,
      width: ScreenUtil().setWidth(750),
      child: Row(
        children: <Widget>[
          selectAllBtn(context),
          allPriceArea(context),
          goButton(context)
        ],
      ),
    );
  }

  //全选按钮
  Widget selectAllBtn(context){
    bool isAllCheck = Provide.value<CartProvide>(context).isAllCheck;
    return Container(
      child: Row(
        children: <Widget>[
          Checkbox(
            value: isAllCheck,
            activeColor: Colors.pink,
            onChanged: (bool val){
              Provide.value<CartProvide>(context).changeAllCheckBtnState(val);
            },
          ),
          Text('全选')
        ],
      ),
    );
  }

  // 合计区域
  Widget allPriceArea(context){

    String allPriceString = Provide.value<CartProvide>(context).allPriceString;
    return Container(
      width: ScreenUtil().setWidth(430),
      alignment: Alignment.centerRight,
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                alignment: Alignment.centerRight,
                width: ScreenUtil().setWidth(150),
                child: Text(
                  '合计:',
                  style:TextStyle(
                    fontSize: ScreenUtil().setSp(36)
                  )
                ), 
              ),
              Container(
                //  alignment: Alignment.centerLeft,
                width: ScreenUtil().setWidth(250),
                child: Text(
                  '￥${allPriceString}元',
                  style:TextStyle(
                    fontSize: ScreenUtil().setSp(36),
                    color: Colors.red,
                  )
                ),
              )  
            ],
          ),
          Container(
            width: ScreenUtil().setWidth(430),
            alignment: Alignment.centerRight,
            child: Text(
              '满10元免配送费，预购免配送费',
              style: TextStyle(
                color: Colors.black38,
                fontSize: ScreenUtil().setSp(22)
              ),
            ),
          )
          
        ],
      ),
    );
  }

  //结算按钮
  Widget goButton(context){

    int allGoodsCount =  Provide.value<CartProvide>(context).allGoodsCount;
    
    return Provide<PersonnalProvider>(
      builder: (context,child,val){
        return Container(
          width: ScreenUtil().setWidth(160),
          padding: EdgeInsets.only(left: 10),
          child:InkWell(
            onTap: (){
              if(val.username=='游客'){
                Fluttertoast.showToast(
                  msg: '请先登录',
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.BOTTOM,
                  timeInSecForIos: 1,
                  backgroundColor: Theme.of(context).primaryColor,
                  textColor: Colors.white,
                  fontSize: 16.0
                );
                return;
              }else if(allGoodsCount==0){
                Fluttertoast.showToast(
                  msg: '请选择商品后再提交',
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.BOTTOM,
                  timeInSecForIos: 1,
                  backgroundColor: Theme.of(context).primaryColor,
                  textColor: Colors.white,
                  fontSize: 16.0
                );
                return;
              }
              Provide.value<CartProvide>(context).postCart(val.token, val.username,context);
            },
            child: Container(
              padding: EdgeInsets.all(10.0),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(3.0)
              ),
              child: Text(
                '提交订单(${allGoodsCount})',
                style: TextStyle(
                  color: Colors.white
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}